
package com.skcc.cop.mrtaxi4driver.biz;


import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;

import com.skcc.cop.mrtaxi4driver.BaseActivity;
import com.skcc.cop.mrtaxi4driver.R;
import com.skcc.cop.mrtaxi4driver.R.color;
import com.skcc.cop.mrtaxi4driver.http.GetCallListListener;
import com.skcc.cop.mrtaxi4driver.http.model.CallList;
import com.skcc.cop.mrtaxi4driver.util.Dialogs;
import com.skcc.cop.mrtaxi4driver.util.Dialogs.DialogListener;
import com.skcc.cop.mrtaxi4driver.util.Variables;
import com.skcc.cop.mrtaxi4driver.view.TitleBar;
import com.skcc.cop.mrtaxi4driver.view.TitleBar.OnTitleBarEventListener;


public class HistoryActivity extends BaseActivity implements OnTitleBarEventListener {
	
	// ===========================================================
	// Constants
	// ===========================================================
	private static final String TAG = HistoryActivity.class.getSimpleName();
	
	private static final int ACCEPT_YES_TAB = 0;
	private static final int ACCEPT_NO_TAB = 1;
	
	
	// ===========================================================
    // Fields
    // ===========================================================
	private TitleBar mTitleBarView;
	
	private TabHost tabHost;
	private ViewPager viewPager;
	private TabsAdapter tabsAdapter;
	
	private Tab YesTab;
	private Tab NoTab;
	
	private OnLoadHistoryListener loadHistoryListener1;
	private OnLoadHistoryListener loadHistoryListener2;

	
    // ===========================================================
    // Methods for/from SuperClass/Interfaces
    // ===========================================================
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_history);
		
		mTitleBarView = (TitleBar) this.findViewById(R.id.titlebar_view);
		mTitleBarView.setEventListener(this);
//		mTitleBarView.showBack();
		mTitleBarView.setTitle(R.string.title);
		
		tabHost = (TabHost)findViewById(android.R.id.tabhost);
		tabHost.setup();
		
		viewPager = (ViewPager)findViewById(R.id.call_history_pager);
		viewPager.setOffscreenPageLimit(1);
		
		tabsAdapter = new TabsAdapter(this, tabHost, viewPager);
		
		YesTab = new Tab(this, R.string.tab_title_agree, Color.RED);
		NoTab = new Tab(this, R.string.tab_title_reject, Color.LTGRAY);
		
		tabsAdapter.addTab(tabHost.newTabSpec("승인내역").setIndicator(YesTab),
				HistoryTab1.HistoryTab1Fragment.class, null);
		tabsAdapter.addTab(tabHost.newTabSpec("거절내역").setIndicator(NoTab),
				HistoryTab2.HistoryTab2Fragment.class, null);
		
	}
																																																																																																																																											
	@Override
	protected void onResume() {
		super.onResume();
		
		Log.d(TAG, " in onResume >>>>>");
	}

	@Override
	protected void onPause() {
		super.onPause();
		
		Log.d(TAG, " in onPause >>>>>");
	}

	@Override
	protected void onDestroy() {
		Log.d(TAG, " in onDestroy >>>>>");
		
		super.onDestroy();
	}
	
	@Override
	public void onBackPressed() {
		finish();
	}

	@Override
	public void onBackClicked() {
		finish();
	}
	
//	@Override
//	public boolean onCreateOptionsMenu(Menu menu) {
//		// Inflate the menu; this adds items to the action bar if it is present.
//		getMenuInflater().inflate(R.menu.main, menu);
//		return true;
//	}
	
	// ===========================================================
    // Methods
    // ===========================================================
	public void getCallList(final String acceptYn) {
		
		showProgressDialog(this, R.string.loading);
		
		gateway.getCallList(acceptYn, new GetCallListListener() {

			@Override
			public void onNoNetwork() {
				dismissProgressDialog();
				
				Dialogs.showDialog(HistoryActivity.this, getString(R.string.no_network), dialogListener);
			}

			@Override
			public void onNoResponse() {
				dismissProgressDialog();
				
				Dialogs.showDialog(HistoryActivity.this, getString(R.string.no_respond), dialogListener);
			}

			@Override
			public void onSessionTimeOut() {
				dismissProgressDialog();
				
				Dialogs.showDialog(HistoryActivity.this, getString(R.string.session_timeout), dialogListener);
			}

			@Override
			public void onSuccess(List<CallList> callList) {
				dismissProgressDialog();
				
				if (Variables.BOOLEAN_VALUE_Y.equals(acceptYn)) {
					if (loadHistoryListener1 != null) {
						loadHistoryListener1.onLoad(callList);
					}
				} else {
					if (loadHistoryListener2 != null) {
						loadHistoryListener2.onLoad(callList);
					}
				}
			}

			@Override
			public void onFail(int code, String msg) {
				dismissProgressDialog();
				
				Dialogs.showDialog(HistoryActivity.this, 
						getString(R.string.server_message) + msg,
						dialogListener);
			}
			
		});
	}
	
	public  void settings(int position) {
		if (position == ACCEPT_YES_TAB) {
			getCallList(Variables.BOOLEAN_VALUE_Y);
			
			YesTab.getTabView().setBackgroundColor(Color.RED);
			NoTab.getTabView().setBackgroundColor(Color.LTGRAY);
		} else if (position == ACCEPT_NO_TAB) {
			getCallList(Variables.BOOLEAN_VALUE_N);
			
			YesTab.getTabView().setBackgroundColor(Color.LTGRAY);
			NoTab.getTabView().setBackgroundColor(Color.GREEN);
		} 
	}
	
	public void setOnLoadHistoryListener1(OnLoadHistoryListener listener) {
		this.loadHistoryListener1 = listener;
	}
	
	public void setOnLoadHistoryListener2(OnLoadHistoryListener listener) {
		this.loadHistoryListener2 = listener;
	}
	
	
	// ===========================================================
    // Inner and Anonymous Classes
    // ===========================================================
	/**
	 * This is a helper class that implements the management of tabs and all
	 * details of connecting a ViewPager with associated TabHost.  It relies on a
	 * trick.  Normally a tab host has a simple API for supplying a View or
	 * Intent that each tab will show.  This is not sufficient for switching
	 * between pages.  So instead we make the content part of the tab host
	 * 0dp high (it is not shown) and the TabsAdapter supplies its own dummy
	 * view to show as the tab content.  It listens to changes in tabs, and takes
	 * care of switch to the correct paged in the ViewPager whenever the selected
	 * tab changes.
	 */
	public static class TabsAdapter extends FragmentPagerAdapter
		implements TabHost.OnTabChangeListener, ViewPager.OnPageChangeListener {
		
		private final Context mContext;
		private final TabHost mTabHost;
		private final ViewPager mViewPager;
		private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();

		static final class TabInfo {
			private final String tag;
			private final Class<?> clss;
			private final Bundle args;

			TabInfo(String _tag, Class<?> _class, Bundle _args) {
				tag = _tag;
				clss = _class;
				args = _args;
			}
		}

		static class DummyTabFactory implements TabHost.TabContentFactory {
			private final Context mContext;

			public DummyTabFactory(Context context) {
				mContext = context;
			}

			@Override
			public View createTabContent(String tag) {
				View v = new View(mContext);
				v.setMinimumWidth(0);
				v.setMinimumHeight(0);
				return v;
			}
		}

		public TabsAdapter(FragmentActivity activity, TabHost tabHost, ViewPager pager) {
			super(activity.getSupportFragmentManager());
			mContext = activity;
			mTabHost = tabHost;
			mViewPager = pager;
			mTabHost.setOnTabChangedListener(this);
			mViewPager.setAdapter(this);
			mViewPager.setOnPageChangeListener(this);
		}

		public void addTab(TabHost.TabSpec tabSpec, Class<?> clss, Bundle args) {
			tabSpec.setContent(new DummyTabFactory(mContext));
			String tag = tabSpec.getTag();
			
			TabInfo info = new TabInfo(tag, clss, args);
			mTabs.add(info);
			mTabHost.addTab(tabSpec);
			notifyDataSetChanged();
		}

		@Override
		public int getCount() {
			return mTabs.size();
		}

		@Override
		public Fragment getItem(int position) {
			TabInfo info = mTabs.get(position);
			return Fragment.instantiate(mContext, info.clss.getName(), info.args);
		}

		@Override
		public void onTabChanged(String tabId) {
			int position = mTabHost.getCurrentTab();
			mViewPager.setCurrentItem(position);
		}

		@Override
		public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

		}

		@Override
		public void onPageSelected(int position) {
			// Unfortunately when TabHost changes the current tab, it kindly
			// also takes care of putting focus on it when not in touch mode.
			// The jerk.
			// This hack tries to prevent this from pulling focus out of our
			// ViewPager.
			TabWidget widget = mTabHost.getTabWidget();
			int oldFocusability = widget.getDescendantFocusability();
			widget.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
			mTabHost.setCurrentTab(position);
			widget.setDescendantFocusability(oldFocusability);

			((HistoryActivity)mContext).settings(position);
		}

		@Override
		public void onPageScrollStateChanged(int state) {
		}

		public ViewPager getViewPager() {
			return mViewPager;
		}

	}

	private class Tab extends LinearLayout
	{
		private View v;

		public Tab(Context context, int regId, int color)
		{
			super(context);
			initTab(regId, color);
		}

		private void initTab(int regId, int color)
		{
			LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = inflater.inflate(R.layout.view_tab, this, true);
			
			TextView title = (TextView) v.findViewById(R.id.tab_title);
			title.setText(regId);
			
			v.setBackgroundColor(color);
		}

		public View getTabView() {
			return v;
		}
	}
	
	private DialogListener dialogListener = new DialogListener() {

		@Override
		public void onClickedOk() {
			Dialogs.dismiss();
		}

		@Override
		public void onClickedCancel() {
			// TODO Auto-generated method stub
			
		}
		
	};
	
	
	interface OnLoadHistoryListener {
		public void onLoad(List<CallList> historyList);
	}
	
}
